package com.xiao.framework.ds.autoconfigure;

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.AbstractDataSource;

/**
 * @author <a href="mailto:xiaoquanidea@163.com">hutiquan</a>
 * @since 2019-08-24 1:08
 */
public abstract class AbstractRoutingDataSource extends AbstractDataSource {

  public abstract DataSource determineDataSource();

  @Override
  @SuppressWarnings("unchecked")
  public <T> T unwrap(Class<T> iface) throws SQLException {
    if (iface.isInstance(this)) {
      return (T) this;
    }
    return determineDataSource().unwrap(iface);
  }

  /**
   *
   * @param iface
   * @return
   * @throws SQLException
   */
  @Override
  public boolean isWrapperFor(Class<?> iface) throws SQLException {
    return (iface.isInstance(this) || determineDataSource().isWrapperFor(iface));
  }

  @Override
  public Connection getConnection() throws SQLException {
    return determineDataSource().getConnection();
  }

  @Override
  public Connection getConnection(String username, String password) throws SQLException {
    return determineDataSource().getConnection(username,password);
  }

}
